package com.parkinglot.service.Impl;


import com.parkinglot.core.Result;
import com.parkinglot.dao.GarageDao;
import com.parkinglot.dao.MonthbilRecordDao;
import com.parkinglot.dao.OrderDao;
import com.parkinglot.parmeter.*;
import com.parkinglot.pojo.Garage;
import com.parkinglot.pojo.MonthbilRecord;
import com.parkinglot.pojo.Order;
import com.parkinglot.service.GarageService;
import com.parkinglot.service.OrderService;
import com.parkinglot.util.HttpUrlutils;
import com.parkinglot.vo.GarageCarCountVo;
import com.parkinglot.vo.GarageOrderVo;
import com.parkinglot.vo.GaragequeryVo;
import com.parkinglot.vo.OrderqueryVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.List;
import java.util.Map;

/**
 * (Garage)表服务实现类
 *
 * @author makejava
 * @since 2020-11-13 15:28:40
 */
@Service("garageService")
@Slf4j
public class GarageServiceImpl implements GarageService {
    @Resource
    private GarageDao garageDao;
    @Resource
    private OrderDao orderDao;
    @Resource
    private OrderService orderService;
    @Resource
    private HttpUrlutils httpUrlutils;
    @Resource
    private MonthbilRecordDao monthbilRecordDao;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public Garage queryById(Integer id) {
        return this.garageDao.queryById(id);
    }

    /**
     * 查询多条数据
     *
     * @param offset 查询起始位置
     * @param limit  查询条数
     * @return 对象列表
     */
    @Override
    public List<Garage> queryAllByLimit(int offset, int limit) {
        return this.garageDao.queryAllByLimit(offset, limit);
    }

    /**
     * 新增数据
     *
     * @param garage 实例对象
     * @return 实例对象
     */
    @Override
    public Garage insert(Garage garage) {
        this.garageDao.insert(garage);
        return garage;
    }

    /**
     * 修改数据
     *
     * @param garage 实例对象
     * @return 实例对象
     */
    @Override
    public Garage update(Garage garage) {
        this.garageDao.update(garage);
        return this.queryById(garage.getId());
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Integer id) {
        return this.garageDao.deleteById(id) > 0;
    }
    /**
     * 新增车库数据
     *
     * @param garageaddParm 实例对象
     * @return 实例对象
     */
    @Override
    public int addGarage(GarageaddParm garageaddParm) {
        Garage garage=new Garage();
        BeanUtils.copyProperties(garageaddParm,garage);
        return this.garageDao.insert(garage);
    }
    /**
     * 删除车库
     *
     * @param garagedelParm 实例对象
     * @return 实例对象
     */
    @Override
    public int delGarage(GaragedelParm garagedelParm) {
        Garage garage=new Garage();
        BeanUtils.copyProperties(garagedelParm,garage);
        return this.garageDao.deleteById(garage.getId());
    }

    @Override
    public int updgarage(GarageupdParm garageupdParm) {
        Garage garage=new Garage();
        BeanUtils.copyProperties(garageupdParm,garage);
        return this.garageDao.update(garage);
    }

    @Override
    public List<GaragequeryVo> querygarage(GaragequeryParm garagequeryParm) {
        Garage garage=new Garage();
        BeanUtils.copyProperties(garagequeryParm,garage);
        List<GaragequeryVo>list=garageDao.queryAll(garage);
        if(list.size()>0){
            return list;
        }
        return Collections.emptyList();
    }

    @Override
    public List<GarageOrderVo> querygarageorderrecord(OrderqueryParm orderqueryParm) {
        return null;
    }

    @Override
    public List<GarageCarCountVo> countgaragecar(GarageCarCountParm garageCarCountParm) {
        List<GarageCarCountVo>list=null;
        try {
           list= orderDao.countgaragecar(garageCarCountParm);
           if(list.size()>0){
               return list;
           }
        }catch (Exception e){
            e.printStackTrace();
        }
        return Collections.emptyList();
    }

    @Override
    public List<GaragequeryVo> allgarage() {
        List<GaragequeryVo>list=null;
        try {
            list=  garageDao.queryallgarage();
            if(list.size()>0)
                return list;
        }catch (Exception e){
            e.printStackTrace();
        }

        return Collections.emptyList();
    }

    @Override
    public GaragequeryVo queryBygaragecode(String garageid) {
        GaragequeryVo garagequeryVo=null;
        try {
            garagequeryVo =garageDao.queryBygaragecode(garageid);
        }catch (Exception e){
            e.printStackTrace();
        }
        return garagequeryVo;
    }

    @Override
    public Result outgarage(GarageUser garageUser) {
        //查询用户订单记录
        Order order=new Order();
        order.setCarnumber(garageUser.getCarnumber());
        order.setUserid(garageUser.getUserid());
        order.setGarageid(garageUser.getGarageCode());
        OrderqueryVo orderqueryVo=orderService.getyufukuanorders(garageUser.getGarageCode(),garageUser.getUserid(),garageUser.getCarnumber());
        //查询该用户是不是月票会员
        MonthbilRecord monthbilRecord= monthbilRecordDao.queryById(garageUser.getGarageCode(),garageUser.getUserid(),garageUser.getCarnumber());
        Map<String, Object> map=null;

        if((orderqueryVo!=null && orderqueryVo.getPayStatus().equals("1")) || monthbilRecord.getPayStatus().equals("11")){
            String tgurl = "http://192.168.3.205:8080/inPark/unlicensedInPark?carNumber="
                    + garageUser.getCarnumber()
                    + "&parkId="
                    + garageUser.getGarageCode();
            map = httpUrlutils.getresult(tgurl);
            return Result.success(map);
        }

        return Result.failure("该用户未支付或者有没开通月票");
    }
}